#!/bin/sh

case "$1" in
  start)
    # Populate the serial number file.
    SERIAL=$(hnvram -qr 1ST_SERIAL_NUMBER)
    [ -n "$SERIAL" ] &&
      echo "$SERIAL" >/etc/serial &&
      chmod 444 /etc/serial

    hostname WindCharger

    # Do this before gpio-mailbox
    update-gpio-links

    # debugfs isn't mounted automatically because this device doesn't use
    # simpleramfs.
    mount -t debugfs none /sys/kernel/debug

    # Start the gpio mailbox ASAP, since it blinks the LEDs to show boot progress.
    echo "Starting gpio-mailbox"
    mkdir -p /tmp/gpio/ledcontrol
    leds 4 0  # blink activity while booting
    ulimit -c 49152

    babysit 60 gpio-mailbox 2>&1 | logos gpio-mailbox &
    wait-until-created /var/run/gpio-mailbox
    pid_gpio_mailbox=$(pidof gpio-mailbox)
    if [ -n "$pid_gpio_mailbox" ]; then
      echo "-1000" > /proc/$pid_gpio_mailbox/oom_score_adj
    fi
    ;;
  stop)
    pkillwait -x gpio-mailbox
    ;;
  restart|reload)
    $0 stop; $0 start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac

